#include "Entry.h"

Entry::Entry()
{

}

// TODO - I hate passing along the planes and airports vectors! Need a better way to do this
Entry::Entry(std::istringstream& entry_stream)
{
	std::string departure;
	std::string destintation;
	std::string call_number;
	std::string buffer;

	std::getline(entry_stream, buffer, ',');
	_year = util::string_to_int(buffer);
	std::getline(entry_stream, _dateStr, ',');
	_date = util::string_to_int(_dateStr);
	_dateStr = util::date_format(buffer, _dateStr);

	std::getline(entry_stream, buffer, ',');
	call_number = util::remove_leading_blanks(buffer);
	std::getline(entry_stream, departure, ',');
	departure = util::remove_leading_blanks(departure);
	std::getline(entry_stream, destintation, ',');
	destintation = util::remove_leading_blanks(destintation);

	std::getline(entry_stream, buffer, ',');            
	_dayLandings = util::string_to_int(buffer);
	std::getline(entry_stream, buffer, ',');            
	_nightLandings = util::string_to_int(buffer);
	std::getline(entry_stream, buffer, ',');
	_approaches = util::string_to_int(buffer);
	std::getline(entry_stream, buffer, ',');            
	_dayHours = util::string_to_double(buffer);
	std::getline(entry_stream, buffer, ',');
	_nightHours = util::string_to_double(buffer);
	std::getline(entry_stream, buffer, ',');                
	_ifr = util::string_to_double(buffer);
	std::getline(entry_stream, buffer, ',');                            
	_gad = util::string_to_double(buffer);
	std::getline(entry_stream, buffer, ',');
	_cfi = util::string_to_double(buffer);
	std::getline(entry_stream, buffer, ',');
	_student = util::string_to_int(buffer);
	std::getline(entry_stream, buffer, ',');            
	_dual = util::string_to_double(buffer);
	std::getline(entry_stream, buffer, ',');
	_pic = util::string_to_double(buffer);
	std::getline(entry_stream, buffer, ',');
	_sic = util::string_to_double(buffer);
	std::getline(entry_stream, buffer, ',');
	_safety = util::string_to_double(buffer);

	//_plane = findPlane(planes, call_number);
	//_departure = findAirport(airports, departure);
	//_destination = findAirport(airports, destintation);
}

Entry::~Entry()
{

}

// TODO - This would be so much faster if we use a hash
const Plane* Entry::findPlane(const std::vector < Plane >& planes, std::string& call_number)
{
	for (int i = 0; i < planes.size(); ++i)
	{
		if (planes[i].callNumber() == call_number)
			return &planes[i];
	}
	return NULL;
}

// TODO - This would be so much faster if we use a hash
const Airport* Entry::findAirport(const std::vector < Airport >& airports, std::string& name)
{
	for (int i = 0; i < airports.size(); ++i)
	{
		if (airports[i].id() == name)
			return &airports[i];
	}
	return NULL;
}

int Entry::index() const
{
	return _index;
}